// pages/transaction/transaction.js

const urlAdd = require('../../utils/config').urlAdd;
const getSumRechargeUrl = require('../../utils/config').getSumRechargeUrl;
const getSumConsumeUrl = require('../../utils/config').getSumConsumeUrl;
const getTradeRecordUrl = require('../../utils/config').getTradeRecordUrl;
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    jiluLists: [],
    listAll: [],
    pageNum: 1,
    pageSize: 10,
    // bom: true,
    // bom2: false,
    noMoretip: false, //false为有更多数据，true为数据加载完毕
    timeStart: '',
    timeEnd: '',
    listNum: 0,   //总条数

    sumRecharge: '0.00',  //总充值
    sumConsume: '0.00',  //总消费
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: "交易记录",
    });
    var that = this;

  },

 

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // 判断是否授权
    var that = this;
    if (wx.getStorageSync('openId') != '') {
      if (wx.getStorageSync('token') == '' || wx.getStorageSync('token') == null) {
        wx.showToast({
          title: '你已授过权, 请登录',
        })
        wx.redirectTo({
          url: '../../pages/userlogin/login?openId=' + wx.getStorageSync('openId')
        })
      } else {
        that.getSumRecharge();  //总充值
        that.getSumConsume();  //总消费
        that.newFindByPage();  //交易记录
      }
    } else {
      wx.showModal({
        title: '提示',
        content: '你还未授权登录，是否授权登录',
        cancelText: '取消',
        cancelColor: 'red',
        confirmText: '确认',
        confirmColor: '#30a1db',
        success: function (res) {
          //点击取消
          if (res.cancel) {
            wx.switchTab({
              url: '../../pages/index/index',
            })
          } else {
            //点击确定
            wx.redirectTo({
              url: '../../pages/userlogin/login'
            })
          }

        },
        fail: function () {}
      })
    }
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log("页面下拉了");
    var that = this;
    var pageNum = that.data.pageNum; //获取现在页码
    if (!that.data.noMoretip) {
      pageNum++
      that.setData({ //页码加一，调用函数，获取下一页内容
        pageNum: pageNum
      })
      that.newFindByPage();
    }
  },
  //查询总充值
  getSumRecharge: function(){
    var that = this;
    wx.request({
      url: getSumRechargeUrl,
      method: 'POST',
      header: {
        'Authorization':'Bearer '+ wx.getStorageSync('token')
      },
      success: function (res) {
        if(res.data.code == 200){
          that.setData({
            sumRecharge: res.data.data,  //总充值
          })
        }
      },
      fail: function () {
        wx.hideLoading();
        wx.showToast({
          title: '系统错误',
          image: '/images/erron.png',
          duration: 2000
        });
      },
      complete: function () {
        wx.hideLoading();
      }
    })
  },

  //查询总消费
  getSumConsume: function(){
    var that = this;
    wx.request({
      url: getSumConsumeUrl,
      method: 'POST',
      header: {
        'Authorization':'Bearer '+ wx.getStorageSync('token')
      },
      success: function (res) {
        if(res.data.code == 200){
          that.setData({
            sumConsume: res.data.data,  //总消费
          })
        }
      },
      fail: function () {
        wx.hideLoading();
        wx.showToast({
          title: '系统错误',
          image: '/images/erron.png',
          duration: 2000
        });
      },
      complete: function () {
        wx.hideLoading();
      }
    })

  },


   //查询记录
   newFindByPage: function () {
    var that = this;
   
    //传过来的账号数据
    wx.showLoading({
      title: '加载中...',
    });
    wx.request({
      url: getTradeRecordUrl,
      data: {
        pageNum: that.data.pageNum,
        pageSize: that.data.pageSize,
        beginTime: that.data.timeStart,
        endTime: that.data.timeEnd
      },
      method: 'POST',
      header: {
        'Authorization':'Bearer '+ wx.getStorageSync('token'),
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        wx.hideLoading();
        var allList = [];
        var listAll = res.data.rows;
        var initList = that.data.jiluLists ? that.data.jiluLists : [];
      
        
        var newList = res.data.rows; //获取新加载的记录
       
        var pageCount = res.data.total%that.data.pageSize==0?res.data.total/that.data.pageSize : parseInt(res.data.total/that.data.pageSize)+1;
        that.setData({
          listNum: res.data.total,
        });
        if ( that.data.pageNum <= 1) { //如果是第一页
          allList = res.data.rows;
        } else {
          allList = initList.concat(newList); //如果不是第一页，连接已加载与新加载记录
        }
        // if (lastPageLength < 15) { //如果新加载的一页记录小于15，则没有下一页
        if ( that.data.pageNum >= pageCount) { //如果新加载的一页记录小于15，则没有下一页
          that.setData({
            noMoretip: true,
          });
        }
        that.setData({
          jiluLists: allList,
          listAll: listAll,
          // bom: true,
          // bom2: false
        })

      },
      fail: function () {
        wx.hideLoading();
        wx.showToast({
          title: '系统错误',
          image: '/images/erron.png',
          duration: 2000
        });
      },
      complete: function () {
        wx.hideLoading();
      }
    })
  },
  //开始日期
  bindTimeChange1: function (e) {
    var that = this;
    this.setData({
      timeStart: e.detail.value
    });
  },
  //结束日期
  bindTimeChange2: function (e) {
    var that = this;
    this.setData({
      timeEnd: e.detail.value
    });
  },

  //按时间查询
  jiaoyijilu: function (e) {
    var that = this;
    that.setData({
      timeStart: e.detail.value.timeStart,
      timeEnd: e.detail.value.timeEnd,
      page: 1,
      jiluLists: [],
      listAll: [],
      listNum: 0
    });
    that.getSumRecharge();  //总充值
    that.getSumConsume();  //总消费
    that.newFindByPage();

  },

  //表单重置
  formReset: function(){
    that.setData({
      timeStart: '',
      timeEnd: '',
    })
  }
})